﻿using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Web.Pay.Models;
using Web.Base.Security;
using Web.Base.Components;
using Ayatta.Contract.Domain;
using Ayatta.Contract.Component;

namespace Web.Pay.Controllers
{
    public class AuctionController : PayBaseController
    {
        public AuctionController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(string param)
        {
            var identity = User.Identity.Identity();

            var order = OrderService.GetOrder(param, identity.Id);
            if (order != null)
            {
                var trade = PaymentService.GetUserTrade(param, identity.Id);
                if (trade != null)
                {
                    string url;
                    if (trade.Status != UserTradeStatus.Pending && order.Status != OrderStatus.Pending)
                    {
                        url = "/order/detail/" + param;
                    }
                    else
                    {
                        url = WebSite.Pay + "/order/?param=" + param;
                    }
                    return Redirect(url);
                }
                var model = new AuctionControllerModel.Index();
                //var locations = MemcachedClient.Put(CacheKey.Extra.Locations, BaseService.GetLocations);

                var addresses = PassportService.GetUserAddresses(identity.Id);
                var address = addresses.OrderByDescending(o => o.IsDefault).FirstOrDefault();
                var invoices = PassportService.GetUserInvoices(identity.Id);
                var invoice = invoices.OrderByDescending(o => o.IsDefault).FirstOrDefault();
                model.Order = order;
                model.UserAddresses = addresses;
                model.UserAddress = address;
                model.UserInvoices = invoices;
                model.UserInvoice = invoice;
                //model.Areas = address;
                return View(model);
            }
            return Redirect(WebSite.My + "/order");
        }

        [HttpPost]
        public ActionResult Index(string param, UserAddress userAddress, UserInvoice userInvoice)
        {
            var identity = User.Identity.Identity();
            var auctionOrder = AuctionService.GetAuctionOrder(param, identity.Id);
            if (auctionOrder.IsVirtual)
            {
                auctionOrder.Receiver = User.Identity.Name;
            }
            else
            {
                auctionOrder.SetAddressInvoice(userAddress, userInvoice);
            }

            var status = AuctionService.UpdateAuctionOrder(auctionOrder);
            if (status)
            {
                var title = "竞购：" + auctionOrder.Name;
                status = PaymentService.CreateUserTrade(auctionOrder.ToTrade(title));
                if (status)
                {
                    var url = WebSite.Pay + "/order/?param=" + param;
                    Redirect(url);
                }
            }
            return Redirect("/auction?param=" + param);
        }

    }
}
